package com.lots.service.impl;

import com.lots.context.BaseContext;
import com.lots.mapper.ActivityMapper;
import com.lots.domain.entity.Activity;
import com.lots.mapper.ActivityReservationMapper;
import com.lots.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;
    @Autowired
    private ActivityReservationMapper activityReservationMapper;

    @Override
    public List<Activity> getActivityList() {
        return activityMapper.getActivityList();
    }

    /**
     * 判断用户是否已经预约过该活动
     *
     * @param activityId
     * @return
     */
    @Override
    public boolean isReserved(Long activityId) {
        Long userId = BaseContext.getCurrentId();
        Long isReserved = activityReservationMapper.selectByUserId(activityId, userId);
        //当前没有记录，用户未预约过该活动，添加记录
        if (isReserved == 0) {
            activityReservationMapper.insertReservation(userId, activityId);
            return true;
        } else {
            //当前没有记录，用户预约过该活动，删除记录
            activityReservationMapper.deleteReservation(userId, activityId);
            return false;
        }

    }

    @Override
    public boolean reservedStatus(Long activityId) {
        //未登录
        Long userId = BaseContext.getCurrentId();
        if (userId == null) {
            return false;
        }
        Long isReserved = activityReservationMapper.selectByUserId(activityId, userId);
        //已预约
        return isReserved != 0;
    }
}
